<template>
  <div class="register">
    <!-- 注册内容 -->
    <div class="register">
      <h3>
        注册新用户
        <span class="go"
          >我有账号，去
          <a
            href="login.html"
            target="_blank"
            @click.prevent="$router.push('/login')"
            >登陆</a
          >
        </span>
      </h3>
      <el-form
        ref="registerForm"
        :model="registerForm"
        status-icon
        :rules="registerRules"
        label-width="80px"
      >
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="registerForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="registerForm.code" class="code-input"></el-input
          ><el-button
            @click="onGetCode"
            :disabled="isSendCodeDisable"
            round
            type="primary"
            >{{ isSendCodeDisable ? `${time}秒` : '发送验证码' }}</el-button
          >
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="registerForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="isAgree" required>
          <el-checkbox v-model="registerForm.isAgree"
            >同意协议并注册《尚品汇用户协议》</el-checkbox
          >
        </el-form-item>
      </el-form>
      <div class="btn">
        <el-button
          round
          @click="onRegister"
          :loading="isRegistering"
          class="reset-el-button"
        >
          {{ isRegistering ? '注册中...' : '注册' }}
        </el-button>
      </div>
    </div>

    <!-- 底部 -->
    <div class="copyright">
      <ul>
        <li>关于我们</li>
        <li>联系我们</li>
        <li>联系客服</li>
        <li>商家入驻</li>
        <li>营销中心</li>
        <li>手机尚品汇</li>
        <li>销售联盟</li>
        <li>尚品汇社区</li>
      </ul>
      <div class="address">地址：北京市昌平区宏福科技园综合楼6层</div>
      <div class="beian">京ICP备19006430号</div>
    </div>
  </div>
</template>

<script>
import { getCode, register } from 'api/register'
export default {
  name: 'Register',
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerForm.confirmPassword !== '') {
          this.$refs.registerForm.validateField('confirmPassword')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const checkIsAgree = (rule, value, callback) => {
      if (!value) {
        callback(new Error('您还没有勾选相关条款'))
      } else {
        callback()
      }
    }
    return {
      // 注册表单
      registerForm: {
        mobile: '',
        code: '',
        password: '',
        confirmPassword: '',
        isAgree: false
      },
      registerRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式不对',
            trigger: 'blur'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        password: [
          { validator: validatePass, trigger: 'blur' },
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        confirmPassword: [
          { validator: validatePass2, trigger: 'blur' },
          { required: true, message: '请再次输入密码', trigger: 'blur' }
        ],
        isAgree: [{ validator: checkIsAgree, trigger: 'change' }]
      },
      isSendCodeDisable: false,
      time: 60,
      // 定时器实例
      timer: null,
      // 是否正在注册
      isRegistering: false
    }
  },
  props: {},
  components: {},
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    /*   获取验证码要对输入的手机号进行正则校验,校验不通过
    直接调elementui表单的validateField方法给予错误提示消息 */
    async onGetCode () {
      const mobileReg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (!mobileReg.test(this.registerForm.mobile)) {
        this.$refs.registerForm.validateField('mobile')
        return
      }
      this.isSendCodeDisable = true
      this.timer = setInterval(() => {
        this.time--
        if (this.time === 0) {
          this.isSendCodeDisable = false
          this.time = 60
          clearInterval(this.timer)
          this.timer = null
        }
      }, 1000)
      const { data: res } = await getCode(this.registerForm.mobile)
      if (res.code === 200) {
        this.$message.success('验证码已发送至手机,请注意查看')
        this.registerForm.code = res.data
      }
    },
    // 注册
    onRegister () {
      this.isRegistering = true
      this.$refs.registerForm.validate(async (flag) => {
        if (!flag) {
          this.isRegistering = false
        } else {
          const { mobile: phone, password, code } = this.registerForm
          console.log(phone, password, code)
          const { data: res } = await register({
            phone,
            password,
            code
          })
          if (res.code === 200) {
            this.$message.success('注册成功')
            this.$router.push('login')
          } else {
            this.$message.warning(res.message)
            this.isRegistering = false
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.register {
  width: 1200px;
  height: 445px;
  border: 1px solid rgb(223, 223, 223);
  margin: 0 auto;
  .el-form {
    width: 500px;
    margin: 30px auto 0px;
    /deep/ .el-input__suffix {
      color: #fb7299;
    }
    .code-input {
      width: 63%;
      margin-right: 20px;
    }
  }
  h3 {
    background: #ececec;
    margin: 0;
    padding: 6px 15px;
    color: #333;
    border-bottom: 1px solid #dfdfdf;
    font-size: 20.04px;
    line-height: 30.06px;

    span {
      font-size: 14px;
      float: right;

      a {
        color: #e1251b;
      }
    }
  }

  .content {
    padding-left: 390px;
    margin-bottom: 18px;
    position: relative;
    .send-code-btn {
      margin-left: 20px;
    }
    &:nth-child(2) {
      margin-top: 40px;
    }
    label {
      font-size: 14px;
      width: 96px;
      text-align: right;
      display: inline-block;
    }

    input {
      width: 270px;
      height: 38px;
      padding-left: 8px;
      box-sizing: border-box;
      margin-left: 5px;
      outline: none;
      border: 1px solid #999;
    }

    img {
      vertical-align: sub;
    }

    .error-msg {
      position: absolute;
      top: 100%;
      left: 495px;
      color: red;
    }
  }

  .controls {
    text-align: center;
    position: relative;

    input {
      vertical-align: middle;
    }

    .error-msg {
      position: absolute;
      top: 100%;
      left: 495px;
      color: red;
    }
  }

  .btn {
    text-align: center;
    line-height: 36px;
    margin: 17px 0 0 55px;

    button {
      outline: none;
      width: 270px;
      height: 36px;
      background: #fb7299;
      color: #fff !important;
      display: inline-block;
      font-size: 16px;
    }
  }
}

.copyright {
  width: 1200px;
  margin: 0 auto;
  text-align: center;
  line-height: 24px;

  ul {
    li {
      display: inline-block;
      border-right: 1px solid #e4e4e4;
      padding: 0 20px;
      margin: 15px 0;
    }
  }
}
</style>
